﻿--非战斗状态中允许shift+左键拖动焦点头像
local function UnitFramesPlus_FocusShiftDrag()
    FocusFrame:SetScript("OnMouseDown", function(self, elapsed)
        if UnitFramesPlusDB["focus"]["movable"] == 1 then
            if IsShiftKeyDown() and (not InCombatLockdown()) then
                FocusFrame:StartMoving();
                UnitFramesPlusVar["focus"]["moving"] = 1;
            end
        end
    end)

    FocusFrame:SetScript("OnMouseUp", function(self, elapsed)
        if UnitFramesPlusVar["focus"]["moving"] == 1 then
            FocusFrame:StopMovingOrSizing();
            UnitFramesPlusVar["focus"]["moving"] = 0;
        end
    end)

    FocusFrame:SetClampedToScreen(1);
end

--头像缩放
local function UnitFramesPlus_FocusFrameScaleSet(oldscale, newscale)
    local oldscale = oldscale or UnitFramesPlusDB["focus"]["scale"];
    local newscale = newscale or UnitFramesPlusDB["focus"]["scale"];
    local point, relativeTo, relativePoint, offsetX, offsetY = FocusFrame:GetPoint();
    FocusFrame:SetScale(newscale);
    FocusFrame:ClearAllPoints();
    FocusFrame:SetPoint(point, relativeTo, relativePoint, offsetX*oldscale/newscale, offsetY*oldscale/newscale);
    if UnitFramesPlusDB["focus"]["portrait"] == 1 and UnitFramesPlusDB["focus"]["ptype"] == 1 then
        UnitFramesPlus_FocusPortraitDisplayUpdate();
    end
end

function UnitFramesPlus_FocusFrameScale(oldscale, newscale)
    if not InCombatLockdown() then
        UnitFramesPlus_FocusFrameScaleSet(oldscale, newscale);
    else
        local func = {};
        func.name = "UnitFramesPlus_FocusFrameScaleSet";
        func.callback = function()
            UnitFramesPlus_FocusFrameScaleSet(oldscale, newscale);            
        end;
        UnitFramesPlus_WaitforCall(func);
    end
end

--焦点生命值百分比
local FocusHPPct = CreateFrame("Frame", "UFP_FocusHPPct", FocusFrame);
FocusHPPct:SetWidth(45);
FocusHPPct:SetHeight(20);
FocusHPPct:ClearAllPoints();
FocusHPPct:SetPoint("RIGHT", FocusFrameHealthBar, "LEFT", -5, -1);
FocusHPPct.Text = FocusHPPct:CreateFontString("UFP_FocusHPPctText", "ARTWORK", "TextStatusBarText");
FocusHPPct.Text:SetAllPoints(FocusHPPct);
FocusHPPct.Text:SetFont(GameFontNormal:GetFont(), 12, "OUTLINE");
FocusHPPct.Text:SetTextColor(1, 0.75, 0);
FocusHPPct.Text:SetJustifyH("RIGHT");
function UnitFramesPlus_FocusHealthPct()
    if UnitFramesPlusDB["focus"]["hppct"] == 1 then
        UFP_FocusHPPct:RegisterEvent("PLAYER_FOCUS_CHANGED");
        UFP_FocusHPPct:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", "focus");
        UFP_FocusHPPct:SetScript("OnEvent", function(self, event, ...)
            if UnitExists("focus") then
                UnitFramesPlus_FocusHealthPctDisplayUpdate();
            end
        end)
    else
        UFP_FocusHPPct.Text:SetText("");
        if UFP_FocusHPPct:IsEventRegistered("PLAYER_FOCUS_CHANGED") then
            UFP_FocusHPPct:UnregisterEvent("PLAYER_FOCUS_CHANGED");
            UFP_FocusHPPct:UnregisterEvent("UNIT_HEALTH_FREQUENT");
            UFP_FocusHPPct:SetScript("OnEvent", nil);
        end
    end
end

--刷新焦点生命值百分比显示
function UnitFramesPlus_FocusHealthPctDisplayUpdate()
    local PctText = "";
    if UnitFramesPlusDB["focus"]["hppct"] == 1 then
        local CurHP = UnitHealth("focus");
        local MaxHP = UnitHealthMax("focus");
        if MaxHP > 0 then
            PctText = math.floor(100*CurHP/MaxHP).."%";
        end
    end
    UFP_FocusHPPct.Text:SetText(PctText);
end

--焦点法力值百分比
local FocusMPPct = CreateFrame("Frame", "UFP_FocusMPPct", FocusFrame);
FocusMPPct:SetWidth(45);
FocusMPPct:SetHeight(20);
FocusMPPct:ClearAllPoints();
FocusMPPct:SetPoint("RIGHT", FocusFrameManaBar, "LEFT", -5, -1);
FocusMPPct.Text = FocusMPPct:CreateFontString("FocusMPPctText", "ARTWORK", "TextStatusBarText");
FocusMPPct.Text:SetAllPoints(FocusMPPct);
FocusMPPct.Text:SetFont(GameFontNormal:GetFont(), 12, "OUTLINE");
FocusMPPct.Text:SetTextColor(1, 1, 1);
FocusMPPct.Text:SetJustifyH("RIGHT");
function UnitFramesPlus_FocusManaPct()
    if UnitFramesPlusDB["focus"]["mppct"] == 1 then
        UFP_FocusMPPct:RegisterEvent("PLAYER_FOCUS_CHANGED");
        UFP_FocusMPPct:RegisterUnitEvent("UNIT_POWER", "focus");
        UFP_FocusMPPct:SetScript("OnEvent", function(self, event, ...)
            if UnitExists("focus") then
                UnitFramesPlus_FocusManaPctDisplayUpdate();
            end
        end)
    else
        UFP_FocusMPPct.Text:SetText("");
        if UFP_FocusMPPct:IsEventRegistered("PLAYER_FOCUS_CHANGED") then
            UFP_FocusMPPct:UnregisterEvent("PLAYER_FOCUS_CHANGED");
            UFP_FocusMPPct:UnregisterEvent("UNIT_POWER");
            UFP_FocusMPPct:SetScript("OnEvent", nil);
        end
    end
end

--刷新焦点法力值百分比显示
function UnitFramesPlus_FocusManaPctDisplayUpdate()
    if UnitFramesPlusDB["focus"]["mppct"] == 1 then
        local MPPct = "";
        local CurMP = UnitPower("focus");
        local MaxMP = UnitPowerMax("focus");
        local powerType = UnitPowerType("focus");
        if powerType == 0 then
            if MaxMP > 0 then
                MPPct = math.floor(100*CurMP/MaxMP).."%";
            end
        end
        UFP_FocusMPPct.Text:SetText(MPPct);
    end
end

--焦点生命条染色
local chb = CreateFrame("Frame");
function UnitFramesPlus_FocusColorHPBar()
    if UnitFramesPlusDB["focus"]["colorhp"] == 1 then
        if UnitFramesPlusDB["focus"]["colortype"] == 1 then
            FocusFrameHealthBar:SetScript("OnValueChanged", nil);
            chb:RegisterEvent("PLAYER_FOCUS_CHANGED");
            chb:SetScript("OnEvent", function(self, event, ...)
                if UnitExists("focus") then
                    UnitFramesPlus_FocusColorHPBarDisplayUpdate();
                end
            end)
        elseif UnitFramesPlusDB["focus"]["colortype"] == 2 then
            if chb:IsEventRegistered("PLAYER_FOCUS_CHANGED") then
                chb:UnregisterEvent("PLAYER_FOCUS_CHANGED");
                chb:SetScript("OnEvent", nil);
            end
            FocusFrameHealthBar:SetScript("OnValueChanged", function(self, value)
                UnitFramesPlus_FocusColorHPBarDisplayUpdate();
            end)
        end
        --FocusFrameHealthBar.lockColor = true;
    else
        FocusFrameHealthBar:SetScript("OnValueChanged", nil);
        if chb:IsEventRegistered("PLAYER_FOCUS_CHANGED") then
            chb:UnregisterEvent("PLAYER_FOCUS_CHANGED");
            chb:SetScript("OnEvent", nil);
        end
        FocusFrameHealthBar:SetStatusBarColor(0, 1, 0);
        --FocusFrameHealthBar.lockColor = nil;
    end
end

--刷新焦点生命条染色显示
function UnitFramesPlus_FocusColorHPBarDisplayUpdate()
    if UnitFramesPlusDB["focus"]["colorhp"] == 1 then
        if UnitFramesPlusDB["focus"]["colortype"] == 1 then
            local HealthBarColor = {r=0, g=1, b=0};
            if UnitIsPlayer("focus") then
                HealthBarColor = RAID_CLASS_COLORS[select(2, UnitClass("focus"))] or {r=0, g=1, b=0};
            end
            FocusFrameHealthBar:SetStatusBarColor(HealthBarColor.r, HealthBarColor.g, HealthBarColor.b);
        elseif UnitFramesPlusDB["focus"]["colortype"] == 2 then
            local CurHP = UnitHealth("focus");
            local MaxHP = UnitHealthMax("focus");
            local Pct = math.floor(100*CurHP/MaxHP)/100;
            local r, g, b = 0, 1, 0;
            if Pct > 0.5 then
                r = (1.0-Pct)*2;
                g = 1.0;
            else
                r = 1.0;
                g = Pct*2;
            end
            if r < 0 then r = 0 end
            if g < 0 then g = 0 end
            if r > 1 then r = 1 end
            if g > 1 then g = 1 end
            FocusFrameHealthBar:SetStatusBarColor(r, g, b);
        end
    end
end

--焦点种族或类型
local FocusType = FocusFrame:CreateFontString("UFP_FocusType", "ARTWORK", "TextStatusBarText");
FocusType:ClearAllPoints();
FocusType:SetPoint("BOTTOMLEFT", FocusFrameNameBackground, "TOPLEFT", 6, 2);
FocusType:SetFont(GameFontNormal:GetFont(), 12, "OUTLINE");
FocusType:SetTextColor(1, 0.75, 0);

local FocusRace = FocusFrame:CreateFontString("UFP_FocusRace", "ARTWORK", "TextStatusBarText");
FocusRace:ClearAllPoints();
FocusRace:SetPoint("BOTTOMLEFT", FocusFrameNameBackground, "TOPLEFT", 6, 2);
FocusRace:SetFont(GameFontNormal:GetFont(), 12, "OUTLINE");
FocusRace:SetTextColor(1, 0.75, 0);

local tr = CreateFrame("Frame");
function UnitFramesPlus_FocusRace()
    if UnitFramesPlusDB["focus"]["race"] == 1 then
        tr:RegisterEvent("PLAYER_FOCUS_CHANGED");
        tr:SetScript("OnEvent", function(self, event)
            if UnitExists("focus") then
                UnitFramesPlus_FocusRaceDisplayUpdate();
            end
        end)
    else
        UFP_FocusType:SetText("");
        UFP_FocusRace:SetText("");
        if tr:IsEventRegistered("PLAYER_FOCUS_CHANGED") then
            tr:UnregisterEvent("PLAYER_FOCUS_CHANGED");
            tr:SetScript("OnEvent", nil);
        end
    end
end

--刷新焦点种族或类型显示
function UnitFramesPlus_FocusRaceDisplayUpdate()
    local typeText = "";
    local raceText = "";
    if UnitFramesPlusDB["focus"]["race"] == 1 then
        if UnitIsPlayer("focus") then
            raceText = UnitRace("focus");
        elseif UnitCreatureType("focus") then
            typeText = UnitCreatureType("focus");
        end
    end
    UFP_FocusType:SetText(typeText);
    UFP_FocusRace:SetText(raceText);
end

--焦点职业图标
local ClassIcon = CreateFrame("Button", "UFP_FocusClassIcon", FocusFrame);
ClassIcon:SetWidth(32);
ClassIcon:SetHeight(32);
ClassIcon:ClearAllPoints();
ClassIcon:SetPoint("TOPLEFT", FocusFrame, "TOPLEFT", 119, 3);
ClassIcon:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight");
ClassIcon:SetAttribute("unit", "focus");
RegisterUnitWatch(ClassIcon);
ClassIcon:SetAlpha(0);

ClassIcon.Border = ClassIcon:CreateTexture("UFP_FocusClassIconBorder", "OVERLAY");
ClassIcon.Border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder");
ClassIcon.Border:SetWidth(54);
ClassIcon.Border:SetHeight(54);
ClassIcon.Border:SetPoint("CENTER", 11, -12);

ClassIcon.Background = ClassIcon:CreateTexture("UFP_FocusClassIconBG", "BORDER");
ClassIcon.Background:SetTexture("Interface\\Minimap\\UI-Minimap-Background");
ClassIcon.Background:SetWidth(20);
ClassIcon.Background:SetHeight(20);
ClassIcon.Background:SetPoint("CENTER");
ClassIcon.Background:SetVertexColor(0, 0, 0, 1);

ClassIcon.Icon = ClassIcon:CreateTexture("UFP_FocusClassIconIcon", "ARTWORK");
ClassIcon.Icon:SetTexture("Interface\\WorldStateFrame\\Icons-Classes");
ClassIcon.Icon:SetWidth(20);
ClassIcon.Icon:SetHeight(20);
ClassIcon.Icon:SetPoint("CENTER");

local fci = CreateFrame("Frame");
function UnitFramesPlus_FocusClassIcon()
    if UnitFramesPlusDB["focus"]["classicon"] == 1 then
        fci:RegisterEvent("PLAYER_FOCUS_CHANGED");
        fci:SetScript("OnEvent", function(self, event)
            if UnitExists("focus") then
                UnitFramesPlus_FocusClassIconDisplayUpdate();
            end
        end)
    else
        UFP_FocusClassIcon:Hide();
        if fci:IsEventRegistered("PLAYER_FOCUS_CHANGED") then
            fci:UnregisterEvent("PLAYER_FOCUS_CHANGED");
            fci:SetScript("OnEvent", nil);
        end
    end
end

--刷新焦点职业图标显示
function UnitFramesPlus_FocusClassIconDisplayUpdate()
    if UnitFramesPlusDB["focus"]["classicon"] == 1 then
        if UnitIsPlayer("focus") then
            local IconCoord = CLASS_ICON_TCOORDS[select(2,UnitClass("focus"))];
            if IconCoord then
                UFP_FocusClassIcon.Icon:SetTexture("Interface\\TargetingFrame\\UI-Classes-Circles");
                UFP_FocusClassIcon.Icon:SetTexCoord(unpack(IconCoord));
            end
            UFP_FocusClassIcon:SetAlpha(1);
        else
            UFP_FocusClassIcon:SetAlpha(0);
        end
    else
        UFP_FocusClassIcon:SetAlpha(0);
    end
end

--焦点职业图标扩展：左键观察/右键交易/中键密语/四号键跟随
local isclicked = false;
local function FocusClassIconDown()
    local point, relativeTo, relativePoint, offsetX, offsetY = UFP_FocusClassIcon.Icon:GetPoint();
    UFP_FocusClassIcon.Icon:ClearAllPoints();
    UFP_FocusClassIcon.Icon:SetPoint(point, relativeTo, relativePoint, offsetX+1, offsetY-1);
    return true;
end

ClassIcon:SetScript("OnMouseDown", function(self, button)
    if UnitFramesPlusDB["focus"]["moreaction"] == 1 then
        if (not UnitCanAttack("player", "focus")) and UnitIsPlayer("focus") then
            if button == "LeftButton" then
                if CheckInteractDistance("focus", 1) then
                    isclicked = FocusClassIconDown();
                    InspectUnit("focus");
                end
            elseif button == "RightButton" then
                if CheckInteractDistance("focus", 2) then
                    isclicked = FocusClassIconDown();
                    InitiateTrade("focus");
                end
            elseif button == "MiddleButton" then
                isclicked = FocusClassIconDown();
                local server = nil;
                local name, server = UnitName("focus");
                local fullname = name;
                if server and (not "focus" or UnitIsSameServer("player", "focus") ~= 1) then
                    fullname = name.."-"..server;
                end
                ChatFrame_SendTell(fullname);
            elseif button == "Button4" then
                if CheckInteractDistance("focus",4) then
                    isclicked = FocusClassIconDown();
                    local server = nil;
                    local name, server = UnitName("focus");
                    local fullname = name;
                    if server and (not "focus" or UnitIsSameServer("player", "focus") ~= 1) then
                        fullname = name.."-"..server;
                    end
                    FollowUnit(fullname, 1);
                end
            end
        end
    end
end)

local function FocusClassIconUp()
    local point, relativeTo, relativePoint, offsetX, offsetY = UFP_FocusClassIcon.Icon:GetPoint();
    UFP_FocusClassIcon.Icon:ClearAllPoints();
    UFP_FocusClassIcon.Icon:SetPoint(point, relativeTo, relativePoint, offsetX-1, offsetY+1);
    return false;
end

ClassIcon:SetScript("OnMouseUp", function(self)
    if UnitFramesPlusDB["focus"]["moreaction"] == 1 and isclicked then
        isclicked = FocusClassIconUp();
    end
end)

--焦点头像内战斗信息
local FocusPortraitIndicator = CreateFrame("Frame", "UFP_FocusPortraitIndicator", FocusFrame);
FocusPortraitIndicator:CreateFontString("UFP_FocusHitIndicator", "OVERLAY", "NumberFontNormalHuge");
UFP_FocusHitIndicator:ClearAllPoints();
UFP_FocusHitIndicator:SetPoint("CENTER", FocusFramePortrait, "CENTER", 0, 0);
CombatFeedback_Initialize(FocusPortraitIndicator, UFP_FocusHitIndicator, 28);
function UnitFramesPlus_FocusPortraitIndicator()
    if UnitFramesPlusDB["focus"]["indicator"] == 1 then
        UFP_FocusPortraitIndicator:RegisterEvent("PLAYER_FOCUS_CHANGED");
        UFP_FocusPortraitIndicator:RegisterUnitEvent("UNIT_COMBAT", "focus");
        UFP_FocusPortraitIndicator:SetScript("OnEvent", function(self, event, ...)
            if event == "PLAYER_FOCUS_CHANGED" then
                UFP_FocusHitIndicator:Hide();
            elseif event == "UNIT_COMBAT" then
                local arg1, arg2, arg3, arg4, arg5 = ...;
                CombatFeedback_OnCombatEvent(self, arg2, arg3, arg4, arg5);
            end
        end)

        UFP_FocusPortraitIndicator:SetScript("OnUpdate", function(self, elapsed)
            CombatFeedback_OnUpdate(self, elapsed);
        end)
    else
        UFP_FocusHitIndicator:Hide();
        if UFP_FocusPortraitIndicator:IsEventRegistered("UNIT_COMBAT") then
            UFP_FocusPortraitIndicator:UnregisterEvent("PLAYER_FOCUS_CHANGED");
            UFP_FocusPortraitIndicator:UnregisterEvent("UNIT_COMBAT");
            UFP_FocusPortraitIndicator:SetScript("OnEvent", nil);
            UFP_FocusPortraitIndicator:SetScript("OnUpdate", nil);
        end
    end
end

--快速焦点，基于slizen的Focuser
local modifierButtons = {"alt", "shift", "ctrl"};
local modifier = "alt";--默认快捷键
local mouseButton = "1";--默认按键：1-左键, 2-右键, 3-中键, 4/5-鼠标快捷键
local actionType = "focus";--默认动作：focus, target
local _G = _G;

local CreateFrame_Hook = function(type, name, parent, template)
    if name and template == "SecureUnitButtonTemplate" then
        _G[name]:SetAttribute(modifier.."-type"..mouseButton, actionType);
    end
end

local frametarget = CreateFrame("CheckButton", "ActionButtonTarget", UIParent, "SecureActionButtonTemplate");
frametarget:SetAttribute("type1", "macro");
frametarget:SetAttribute("macrotext", "/target mouseover");

hooksecurefunc("CreateFrame", CreateFrame_Hook)
local framefocus = CreateFrame("CheckButton", "ActionButton", UIParent, "SecureActionButtonTemplate");
framefocus:SetAttribute("type1", "macro");
framefocus:SetAttribute("macrotext", "/focus mouseover");

local duf = {
    PetFrame,
    PartyMemberFrame1,
    PartyMemberFrame2,
    PartyMemberFrame3,
    PartyMemberFrame4,
    PartyMemberFrame1PetFrame,
    PartyMemberFrame2PetFrame,
    PartyMemberFrame3PetFrame,
    PartyMemberFrame4PetFrame,
    PartyTarget1,
    PartyTarget2,
    PartyTarget3,
    PartyTarget4,
    TargetFrame,
    TargetFrameToT,
    TargetofTargetTargetFrame,
}

function UnitFramesPlus_FocusQuickClear(button)
    actionType = "target";
    modifier = button;
    SetOverrideBindingClick(ActionButtonTarget, true, modifier.."-BUTTON"..mouseButton, "ActionButtonTarget");
    for i, frame in pairs(duf) do
        frame:SetAttribute(modifier.."-type"..mouseButton, actionType);
    end
end

function UnitFramesPlus_FocusQuickInit()
    if UnitFramesPlusDB["focus"]["quick"] == 1 then
        actionType = "focus";
        modifier = modifierButtons[UnitFramesPlusDB["focus"]["button"]];
        SetOverrideBindingClick(ActionButton, true, modifier.."-BUTTON"..mouseButton, "ActionButton");
        for i, frame in pairs(duf) do
            frame:SetAttribute(modifier.."-type"..mouseButton, actionType);
        end
    end
end

function UnitFramesPlus_FocusQuick()
    if UnitFramesPlusDB["focus"]["quick"] == 1 then
        UnitFramesPlus_FocusQuickInit();
    else
        UnitFramesPlus_FocusQuickClear(modifierButtons[UnitFramesPlusDB["focus"]["button"]]);
    end
end

--焦点头像
local Focus3DPortrait = CreateFrame("PlayerModel", "UFP_Focus3DPortrait", FocusFrame);
Focus3DPortrait:SetWidth(50);
Focus3DPortrait:SetHeight(50);
Focus3DPortrait:SetFrameLevel(1);
Focus3DPortrait:ClearAllPoints();
Focus3DPortrait:SetPoint("CENTER", FocusFramePortrait, "CENTER", -1, -1);
Focus3DPortrait:Hide();
Focus3DPortrait.Background = Focus3DPortrait:CreateTexture("UFP_Focus3DPortraitBG", "BACKGROUND");
Focus3DPortrait.Background:SetTexture("Interface\\AddOns\\UnitFramesPlus\\Portrait3D");
Focus3DPortrait.Background:SetWidth(64);
Focus3DPortrait.Background:SetHeight(64);
Focus3DPortrait.Background:ClearAllPoints();
Focus3DPortrait.Background:SetPoint("CENTER", Focus3DPortrait, "CENTER", 0, 0);
Focus3DPortrait.Background:Hide();

local FocusClassPortrait = FocusFrame:CreateTexture("UFP_FocusClassPortrait", "ARTWORK");
FocusClassPortrait:SetWidth(64);
FocusClassPortrait:SetHeight(64);
FocusClassPortrait:ClearAllPoints();
FocusClassPortrait:SetPoint("TOPRIGHT", FocusFrame, "TOPRIGHT", -42, -12);
FocusClassPortrait:Hide();

local fpt = CreateFrame("Frame");
function UnitFramesPlus_FocusPortrait()
    if UnitFramesPlusDB["focus"]["portrait"] == 1 then
        FocusFramePortrait:Hide();
        if UnitFramesPlusDB["focus"]["ptype"] == 1 then
            UFP_Focus3DPortrait:Show();
            UFP_FocusClassPortrait:Hide();
            UnitFramesPlus_FocusPortrait3DBGDisplayUpdate();
            fpt:RegisterEvent("PLAYER_FOCUS_CHANGED");
            fpt:RegisterUnitEvent("UNIT_MODEL_CHANGED", "focus");
            fpt:RegisterUnitEvent("UNIT_CONNECTION", "focus");
            fpt:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", "focus");
            fpt:SetScript("OnEvent", function(self, event, ...)
                if event == "PLAYER_FOCUS_CHANGED" then
                    if UnitExists("focus") then
                        if UnitFramesPlusDB["focus"]["p3dbg"] == 1 then
                            local color = RAID_CLASS_COLORS[select(2, UnitClass("focus"))] or NORMAL_FONT_COLOR;
                            UFP_Focus3DPortrait.Background:SetVertexColor(color.r/1.5, color.g/1.5, color.b/1.5, 1);
                        end
                        UnitFramesPlus_FocusPortraitDisplayUpdate();
                    end
                elseif event == "UNIT_MODEL_CHANGED" or event == "UNIT_CONNECTION" then
                    UnitFramesPlus_FocusPortraitDisplayUpdate();
                elseif event == "UNIT_HEALTH_FREQUENT" then
                    if (not UnitIsConnected("focus")) or UnitIsGhost("focus") then
                        UFP_Focus3DPortrait:SetLight(true, false, 0, 0, 0, 1.0, 0.25, 0.25, 0.25);
                    elseif UnitIsDead("focus") then
                        UFP_Focus3DPortrait:SetLight(true, false, 0, 0, 0, 1.0, 1, 0.3, 0.3);
                    else
                        UFP_Focus3DPortrait:SetLight(true, false, 0, 0, 0, 1.0, 1, 1, 1);
                    end
                end
            end)
        elseif UnitFramesPlusDB["focus"]["ptype"] == 2 then
            UFP_Focus3DPortrait:Hide();
            UFP_FocusClassPortrait:Show();
            if not fpt:IsEventRegistered("PLAYER_FOCUS_CHANGED") then
                fpt:RegisterEvent("PLAYER_FOCUS_CHANGED");
            else
                if fpt:IsEventRegistered("UNIT_MODEL_CHANGED") then
                    fpt:UnregisterEvent("UNIT_MODEL_CHANGED");
                    fpt:UnregisterEvent("UNIT_CONNECTION");
                    fpt:UnregisterEvent("UNIT_HEALTH_FREQUENT");
                end
            end
            fpt:SetScript("OnEvent", function(self, event, ...)
                if event == "PLAYER_FOCUS_CHANGED" then
                    if UnitExists("focus") then
                        UnitFramesPlus_FocusPortraitDisplayUpdate();
                    end
                end
            end)
        end
        UnitFramesPlus_FocusPortraitDisplayUpdate();
    else
        FocusFramePortrait:Show();
        UFP_Focus3DPortrait:Hide();
        UFP_FocusClassPortrait:Hide();
        if fpt:IsEventRegistered("PLAYER_FOCUS_CHANGED") then
            fpt:UnregisterEvent("PLAYER_FOCUS_CHANGED");
            fpt:UnregisterEvent("UNIT_MODEL_CHANGED");
            fpt:UnregisterEvent("UNIT_CONNECTION");
            fpt:UnregisterEvent("UNIT_HEALTH_FREQUENT");
            fpt:SetScript("OnEvent", nil);
        end
    end
end

--刷新焦点头像显示
function UnitFramesPlus_FocusPortraitDisplayUpdate()
    if UnitFramesPlusDB["focus"]["ptype"] == 1 then
        if (not UnitIsConnected("focus")) or (not UnitIsVisible("focus")) then
            UFP_Focus3DPortrait:SetPortraitZoom(0);
            UFP_Focus3DPortrait:SetCamDistanceScale(0.25);
            UFP_Focus3DPortrait:SetPosition(0,0,0.5);
            UFP_Focus3DPortrait:ClearModel();
            UFP_Focus3DPortrait:SetModel("Interface\\Buttons\\TalkToMeQuestionMark.M2");
        else
            UFP_Focus3DPortrait:SetPortraitZoom(1);
            UFP_Focus3DPortrait:SetCamDistanceScale(1);
            UFP_Focus3DPortrait:SetPosition(0,0,0);
            UFP_Focus3DPortrait:ClearModel();
            UFP_Focus3DPortrait:SetUnit("focus");
            if (not UnitIsConnected("focus")) or UnitIsGhost("focus") then
                UFP_Focus3DPortrait:SetLight(true, false, 0, 0, 0, 1.0, 0.25, 0.25, 0.25);
            elseif UnitIsDead("focus") then
                UFP_Focus3DPortrait:SetLight(true, false, 0, 0, 0, 1.0, 1, 0.3, 0.3);
            else
                UFP_Focus3DPortrait:SetLight(true, false, 0, 0, 0, 1.0, 1, 1, 1);
            end
        end
    elseif UnitFramesPlusDB["focus"]["ptype"] == 2 then
        if UnitFramesPlusDB["focus"]["npcno"] == 1 and not UnitIsPlayer("focus") then
            if UFP_FocusClassPortrait:IsShown() then
                FocusFramePortrait:Show();
                UFP_FocusClassPortrait:Hide();
            end
        else
            if FocusFramePortrait:IsShown() then
                FocusFramePortrait:Hide();
                UFP_FocusClassPortrait:Show();
            end
            local IconCoord = CLASS_ICON_TCOORDS[select(2,UnitClass("focus"))]
            if IconCoord then
                UFP_FocusClassPortrait:SetTexture("Interface\\TargetingFrame\\UI-Classes-Circles");
                UFP_FocusClassPortrait:SetTexCoord(unpack(IconCoord));
            end
        end
    end
end

--刷新焦点3D头像背景显示
function UnitFramesPlus_FocusPortrait3DBGDisplayUpdate()
    if UnitFramesPlusDB["focus"]["portrait"] == 1 
    and UnitFramesPlusDB["focus"]["ptype"] == 1
    and UnitFramesPlusDB["focus"]["p3dbg"] == 1 then
        UFP_Focus3DPortrait.Background:Show();
    else
        UFP_Focus3DPortrait.Background:Hide();
    end
end

--鼠标移过时才显示数值
function UnitFramesPlus_FocusBarTextMouseShow()
    if UnitFramesPlusDB["focus"]["mouseshow"] == 1 then
        FocusFrameTextureFrameHealthBarText:SetAlpha(0);
        FocusFrameTextureFrameHealthBarTextLeft:SetAlpha(0);
        FocusFrameTextureFrameHealthBarTextRight:SetAlpha(0);
        FocusFrameTextureFrameManaBarText:SetAlpha(0);
        FocusFrameTextureFrameManaBarTextLeft:SetAlpha(0);
        FocusFrameTextureFrameManaBarTextRight:SetAlpha(0);
        FocusFrameTextureFrame:SetScript("OnEnter",function(self)
            FocusFrameTextureFrameHealthBarText:SetAlpha(1);
            FocusFrameTextureFrameHealthBarTextLeft:SetAlpha(1);
            FocusFrameTextureFrameHealthBarTextRight:SetAlpha(1);
            FocusFrameTextureFrameManaBarText:SetAlpha(1);
            FocusFrameTextureFrameManaBarTextLeft:SetAlpha(1);
            FocusFrameTextureFrameManaBarTextRight:SetAlpha(1);
        end);
        FocusFrameTextureFrame:SetScript("OnLeave",function()
            FocusFrameTextureFrameHealthBarText:SetAlpha(0);
            FocusFrameTextureFrameHealthBarTextLeft:SetAlpha(0);
            FocusFrameTextureFrameHealthBarTextRight:SetAlpha(0);
            FocusFrameTextureFrameManaBarText:SetAlpha(0);
            FocusFrameTextureFrameManaBarTextLeft:SetAlpha(0);
            FocusFrameTextureFrameManaBarTextRight:SetAlpha(0);
        end);
    else
        FocusFrameTextureFrameHealthBarText:SetAlpha(1);
        FocusFrameTextureFrameHealthBarTextLeft:SetAlpha(1);
        FocusFrameTextureFrameHealthBarTextRight:SetAlpha(1);
        FocusFrameTextureFrameManaBarText:SetAlpha(1);
        FocusFrameTextureFrameManaBarTextLeft:SetAlpha(1);
        FocusFrameTextureFrameManaBarTextRight:SetAlpha(1);
        FocusFrameTextureFrame:SetScript("OnEnter",nil);
        FocusFrameTextureFrame:SetScript("OnLeave",nil);
    end
end

--模块初始化
function UnitFramesPlus_FocusInit()
    UnitFramesPlus_FocusShiftDrag();
    UnitFramesPlus_FocusHealthPct();
    UnitFramesPlus_FocusManaPct();
    UnitFramesPlus_FocusRace();
    UnitFramesPlus_FocusClassIcon();
    UnitFramesPlus_FocusColorHPBar();
    UnitFramesPlus_FocusPortraitIndicator();
    UnitFramesPlus_FocusQuickInit();
    UnitFramesPlus_FocusPortrait();
    UnitFramesPlus_FocusFrameScale();
    UnitFramesPlus_FocusBarTextMouseShow();
end
